package com.e.ticket;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import com.e.Adapter.TickeAdapter;
import com.e.Pojo.Tickes;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class Tickes_Item extends AppCompatActivity {

    int year,month,days,week;
    private ListView listView;
    private TickeAdapter adapter;
    private List<Tickes> tickes;
    final String FILE_NAME="user";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tickes_item);
        listView = (ListView) findViewById(R.id.list_view);
        this.setTitle("校车预约");
        Intent intent = getIntent();
        year = intent.getExtras().getInt("year");
        month = intent.getExtras().getInt("month")+1;
        days = intent.getExtras().getInt("days");
        week = intent.getExtras().getInt("week")-1;
        int id = intent.getExtras().getInt("id");
        tickes = lists(year,month,days,week,id);
        System.out.println(tickes.toString());
        SharedPreferences sp =getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        String sno = sp.getString("sno","");
        if (tickes.size()==0){
            Toast.makeText(Tickes_Item.this, "今天已无校车", Toast.LENGTH_SHORT).show();
        }
        adapter = new TickeAdapter(Tickes_Item.this,tickes,sno);
        listView.setAdapter(adapter);




    }

    public List<Tickes> lists(int year,int month,int days,int week,int id){
        List<Tickes> tickes = new ArrayList<>();
        Calendar calendar1  = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
       format.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Date data = new Date(System.currentTimeMillis());
        String  s = format.format(data);
        if (id == 1 && (week==1 || week==2 || week==3 || week==4)){
            String  [] times = {"08:00","10:30","13:00","15:20","17:10","17:10","17:10","17:55"};
            if (checkDate()){
                System.out.println("-------------->"+s);
                for (String time : times) {
                    if (s.compareTo(time)<0) {
                        Tickes tickes1 = new Tickes("金堂校区", "洪河校区",Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                        tickes.add(tickes1);
                    }
                }
            }else{
                for (String time : times){
                    Tickes tickes1 = new Tickes("金堂校区", "洪河校区", Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                    tickes.add(tickes1);
                }

            }
            return tickes;
        }else if(id == 2 && (week==1 || week==2 || week==3 || week==4)){
            String  [] times = {"07:10","09:00","11:30","14:00","16:00"};
            if (checkDate()){
                for (String time : times) {
                    if (s.compareTo(time)<0) {
                        Tickes tickes1 = new Tickes("洪河校区", "金堂校区",Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                        tickes.add(tickes1);
                    }
                }
            }else{
                for (String time : times){
                    Tickes tickes1 = new Tickes("洪河校区", "金堂校区", Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                    tickes.add(tickes1);
                }

            }
            return tickes;
        }else if(id==1 && week == 5){
            String  [] times = {"08:00","10:30","13:00","15:30","17:10","17:10","17:10","17:55"};
            if (checkDate()){
                for (String time : times) {
                    if (s.compareTo(time)<0) {
                        Tickes tickes1 = new Tickes("金堂校区", "洪河校区",Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                        tickes.add(tickes1);
                    }
                }
            }else{
                for (String time : times){
                    Tickes tickes1 = new Tickes("金堂校区", "洪河校区", Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                    tickes.add(tickes1);
                }

            }
            return tickes;
        }else if(id==2 && week == 5){
            String  [] times = {"07:10","09:00","11:30","14:00","16:30"};
            if (checkDate()){
                for (String time : times) {
                    if (s.compareTo(time)<0) {
                        Tickes tickes1 = new Tickes("洪河校区", "金堂校区",Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                        tickes.add(tickes1);
                    }
                }
            }else{
                for (String time : times){
                    Tickes tickes1 = new Tickes("洪河校区", "金堂校区", Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                    tickes.add(tickes1);
                }
            }
            return tickes;
        }else if(id==1 && (week==6 || week==7)){
            String  [] times = {"09:30","17:30"};
            if (checkDate()){
                for (String time : times) {
                    if (s.compareTo(time)<0) {
                        Tickes tickes1 = new Tickes("金堂校区", "洪河校区",Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                        tickes.add(tickes1);
                    }
                }
            }else{
                for (String time : times){
                    Tickes tickes1 = new Tickes("金堂校区", "洪河校区", Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                    tickes.add(tickes1);
                }
            }
            return tickes;
        }else if(id==2 && (week==6 || week==7)){
            String  [] times = {"08:30","16:30"};
            if (checkDate()){
                for (String time : times) {
                    if (s.compareTo(time)<0) {
                        Tickes tickes1 = new Tickes("金堂校区", "洪河校区",Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                        tickes.add(tickes1);
                    }
                }
            }else{
                for (String time : times){
                    Tickes tickes1 = new Tickes("金堂校区", "洪河校区", Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(days)+" "+time);
                    tickes.add(tickes1);
                }

            }
            return tickes;
        }
        return tickes;
    }

    public boolean checkDate(){
        Calendar calendar  = Calendar.getInstance();
        int year_1 = calendar.get(Calendar.YEAR);
        int month_1 = calendar.get(Calendar.MONTH)+1;
        int days_1 = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println("--------------------------------------------->"+days_1);
        return year == year_1 && month == month_1 && days == days_1;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.back, menu);
        return true;
    }
    //onOptionsItemSelected

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_cart://监听菜单按钮
                Intent intent = new Intent(Tickes_Item.this,MainActivity.class);
                Bundle b = new Bundle();
                b.putInt("ids",1);
                intent.putExtras(b);
                startActivity(intent);

//                Toast.makeText(Tickes_Item.this,"你好",Toast.LENGTH_SHORT).show();
//                Intent intent =new Intent(Update_password.this,Own.class);
//                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}